package br.com.lucasddaniel.merchanmobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.lucasddaniel.merchanmobile.R;
import br.com.lucasddaniel.merchanmobile.bean.TuristaBean;

public class TuristaSelecionadoAdapter extends ArrayAdapter<TuristaBean> {
	private Context context = null;
	private List<TuristaBean> listTuristasBean = null;
	private List<TuristaBean> items;

	public List<TuristaBean> getItems() {
		return items;
	}

	public void setItems( List<TuristaBean> items ) {
		this.items = items;
	}

	public List<TuristaBean> getListTuristasBean() {
		return listTuristasBean;
	}

	public void setListTuristasBean( List<TuristaBean> listTuristasBean ) {
		this.listTuristasBean = listTuristasBean;
	}

	private MyFilter filter;

	class MyFilter extends Filter {

		private TuristaSelecionadoAdapter adapter;

		public MyFilter( TuristaSelecionadoAdapter adapter ) {
			this.adapter = adapter;
		}

		@Override
		protected FilterResults performFiltering( CharSequence constraint ) {
			FilterResults results = new FilterResults();

			if( constraint.toString().equals( "" ) ) {
				results.values = adapter.getListTuristasBean();
				results.count = adapter.getListTuristasBean().size();
				return results;
			}
			List<TuristaBean> filteredItems = new ArrayList<TuristaBean>();
			for( TuristaBean o: adapter.getListTuristasBean() ) {
				if( o.getNome().toUpperCase().contains( constraint.toString().toUpperCase() ) ) {
					filteredItems.add( o );
				}
			}

			results.values = filteredItems;
			results.count = filteredItems.size();
			return results;
		}

		@SuppressWarnings( "unchecked" )
		@Override
		protected void publishResults( CharSequence constraint, FilterResults results ) {
			adapter.setItems( ( List<TuristaBean> ) results.values );
		}
	}

	public TuristaSelecionadoAdapter( Context context, int resource,
			List<TuristaBean> listTuristasBean ) {
		super( context, resource, listTuristasBean );
		this.context = context;
		this.listTuristasBean = listTuristasBean;
	}

	@Override
	public View getView( int position, View convertView, ViewGroup parent ) {
		LayoutInflater inflater = ( LayoutInflater ) context
				.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View rowView = inflater.inflate(
				br.com.lucasddaniel.merchanmobile.R.layout.row_turista_selecionado, parent,
				false );
		if( listTuristasBean != null && listTuristasBean.size() != 0 ) {
			TuristaBean turistasBean = listTuristasBean.get( position );
			if( turistasBean != null ) {
				TextView txtViewNome = ( TextView ) rowView
						.findViewById( R.id_row_turista_selecionado.turistas_nome );
				TextView txtViewVooIn = ( TextView ) rowView
						.findViewById( R.id_row_turista_selecionado.turistas_voo_in );
				TextView txtViewPousada = ( TextView ) rowView
						.findViewById( R.id_row_turista_selecionado.turistas_pousada );
				ImageView imgChecked = ( ImageView ) rowView.findViewById( R.id_row_turista_selecionado.img );
				turistasBean.setImg( imgChecked );
				txtViewNome.setText( turistasBean.getNome() );
				txtViewVooIn.setText( turistasBean.getVooIn() );
				txtViewPousada.setText( turistasBean.getPousada() );
				if( turistasBean.isChecked() ) {
					turistasBean.getImg().setVisibility( View.VISIBLE );
				} else {
					turistasBean.getImg().setVisibility( View.INVISIBLE );
				}
			}
		}
		return rowView;
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public Filter getFilter() {
		if( this.filter == null )
			this.filter = new MyFilter( this );
		return this.filter;
	}
}
